package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.InsuranceConditionVO;
import com.itheima.sfbx.insurance.enums.InsuranceConditionEnum;
import com.itheima.sfbx.insurance.mapper.InsuranceConditionMapper;
import com.itheima.sfbx.insurance.pojo.InsuranceCondition;
import com.itheima.sfbx.insurance.service.IInsuranceConditionService;
import com.itheima.sfbx.insurance.service.IInsuranceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description：保险筛选项服务实现类
 */
@Slf4j
@Service
public class InsuranceConditionServiceImpl extends ServiceImpl<InsuranceConditionMapper, InsuranceCondition> implements IInsuranceConditionService {

    @Autowired
    private IInsuranceService insuranceService;


    @Override
    @Transactional
    public Boolean save(List<InsuranceConditionVO> insuranceConditionListVOs) {
        //查询
        if (CollUtil.isEmpty(insuranceConditionListVOs)){
            log.error("数据为空!");
            return null;
        }
        Integer count = this.lambdaQuery()
                .eq(InsuranceCondition::getInsuranceId, insuranceConditionListVOs.get(0).getInsuranceId())
                .in(InsuranceCondition::getConditionKey, insuranceConditionListVOs.stream().map( InsuranceConditionVO::getConditionKey).collect(Collectors.toSet()))
                .count();
        if (count>0){
            log.error("保存项已存在！{}",insuranceConditionListVOs);
            throw new ProjectException(InsuranceConditionEnum.SAVE_FAIL);
        }
        return this.saveBatch(BeanConv.toBeanList(insuranceConditionListVOs,InsuranceCondition.class));
    }

    @Override
    @Transactional
    public Boolean update(List<InsuranceConditionVO> insuranceConditionVOs) {
        //查询筛选项是否存
        if (CollUtil.isEmpty(insuranceConditionVOs)){
            log.error("数据为空!");
            return null;
        }
        Integer count = lambdaQuery()
                .in(InsuranceCondition::getId, insuranceConditionVOs.stream().map(InsuranceConditionVO::getId).collect(Collectors.toSet()))
                .count();
        if (count<insuranceConditionVOs.size()){
            log.error("数据库中不存在！{}",insuranceConditionVOs);
            throw new ProjectException(InsuranceConditionEnum.UPDATE_FAIL);
        }
        boolean update = this.lambdaUpdate()
                .eq(InsuranceCondition::getId, insuranceConditionVOs.stream().map(InsuranceConditionVO::getId).collect(Collectors.toSet()))
                .update();
        if (!update){
            throw new ProjectException(InsuranceConditionEnum.UPDATE_FAIL);
        }
        return true;
    }


    @Override
    public List<InsuranceConditionVO> findList(InsuranceConditionVO insuranceConditionVO) {
        List<InsuranceCondition> list = this.lambdaQuery()
                .eq(InsuranceCondition::getConditionKey, insuranceConditionVO.getConditionKey())
                .like(InsuranceCondition::getConditionKeyName, insuranceConditionVO.getConditionKeyName())
                .eq(InsuranceCondition::getSortNo, insuranceConditionVO.getSortNo())
                .eq(InsuranceCondition::getDataState, insuranceConditionVO.getDataState())
                .list();
        return BeanConv.toBeanList(list, InsuranceConditionVO.class);
    }

}
